/*
 * Copyright 2008 echo.khannedy@gmail.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * ["Let's ROCK with JAVA"]
 * 
 * ["echo.khannedy@gmail.com"]
 * ["http://eecchhoo.wordpress.com/"]
 */
package open.usu.swing;

import java.awt.Component;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import javax.swing.JLayeredPane;

/**
 * EGlassPane merupakan komponen untuk glasspane yang berguna sebagai container
 * untuk komponen - komponen, karena merupakan subclass dari {@link JLayeredPane}
 * 
 * @author usu
 * @author <a href="mailto:echo.khannedy@gmail.com">echo.khannedy@gmail.com</a>
 * @see <a href="http://eecchhoo.wordpress.com/">http://eecchhoo.wordpress.com/</a>
 * @see <a href="http://biosmansa.wordpress.com/">http://biosmansa.wordpress.com/</a>
 * @see <a href="http://code.google.com/p/eglasspane/">http://code.google.com/p/eglasspane/</a>
 */
public class EGlassPane extends JLayeredPane {

    private static final long serialVersionUID = -8708254113627215002L;

    /**
     * Membuat EGlasspane baru
     */
    public EGlassPane() {
        // Panggil superclass
        super();

        // Mengubah Opaque menjadi false agar transparan
        super.setOpaque(false);

        // Menambah aksi komponen ketika struktur komponen berubah
        addComponentListener(new ResizeListener(this));
    }

    /**
     * Menambah komponen ke dalam EGlassPane
     * @param comp komponen yang akan ditambahkan
     */
    public void addGlasspaneComponent(Component comp) {
        // Menambah komponen
        add(comp);
    }

    @Override
    public Component add(Component comp) {
        Component c = super.add(comp);

        // Mengubah lokasi dan besar komponen agar sama dengan EGlassPane
        comp.setBounds(0, 0, getWidth(), getHeight());
        return c;
    }

    @Override
    @Deprecated
    public void setOpaque(boolean isOpaque) {
        super.setOpaque(false);
    }
    // <editor-fold defaultstate="collapsed" desc="Inner Class">
    /**
     * Listener untuk aksi komponen resize
     * 
     * @author usu
     * @author <a href="mailto:echo.khannedy@gmail.com">echo.khannedy@gmail.com</a>
     * @see <a href="http://eecchhoo.wordpress.com/">http://eecchhoo.wordpress.com/</a>
     * @see <a href="http://biosmansa.wordpress.com/">http://biosmansa.wordpress.com/</a>
     * @see <a href="http://code.google.com/p/eglasspane/">http://code.google.com/p/eglasspane/</a>
     */
    public static class ResizeListener implements ComponentListener {

        private EGlassPane glasspane;

        /**
         * Membuat COmponent Resize Listener baru
         * @param glasspane
         */
        public ResizeListener(EGlassPane glasspane) {
            this.glasspane = glasspane;
        }

        @Override
        public void componentResized(ComponentEvent e) {
            for (Component comp : glasspane.getComponents()) {
                // Mengubah ukuran komponen agar sama dengan glasspane
                comp.setBounds(0, 0, glasspane.getWidth(), glasspane.getHeight());
                comp.repaint();
            }
        }

        @Override
        public void componentMoved(ComponentEvent e) {
            componentResized(e);
        }

        @Override
        public void componentShown(ComponentEvent e) {
            componentResized(e);
        }

        @Override
        public void componentHidden(ComponentEvent e) {
            componentResized(e);
        }
    }
// </editor-fold>
}
